﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ZLDC.Common.Estate;
using ZLDC.Topo.Model.Estate;

namespace ZLDC.DAL.Estate
{
    public class SystemOptionDAL
    {
                                                        public static DataTable p_system_option_listall_table(int typeid, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@typeid", typeid));
            SqlParameter pRetval = new SqlParameter("@retval", SqlDbType.Int, 4);
            pRetval.Direction = ParameterDirection.ReturnValue;
            parms.Add(pRetval);

            DataTable dt = null;
            dt = db.GetDataTable("p_system_option_listall", parms);

            int retval = (pRetval.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pRetval.Value);

            return dt;
        }

                                                                                                                                                public static void p_system_option_save(int ID, int subsidiaryID, string subsidiary, int typeid, string item, int itemValue, string itemFieldName, string itemField, int dltFlg, string modifyUser, string remark,int grade, out string outmsg, out int newid, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@ID", ID));
            parms.Add(new SqlParameter("@subsidiaryID", subsidiaryID));
            parms.Add(new SqlParameter("@subsidiary", subsidiary));
            parms.Add(new SqlParameter("@typeid", typeid));
            parms.Add(new SqlParameter("@item", item));
            parms.Add(new SqlParameter("@itemValue", itemValue));
            parms.Add(new SqlParameter("@itemFieldName", itemFieldName));
            parms.Add(new SqlParameter("@itemField", itemField));
            parms.Add(new SqlParameter("@dltFlg", dltFlg));
            parms.Add(new SqlParameter("@modifyUser", modifyUser));
            parms.Add(new SqlParameter("@remark", remark));
            parms.Add(new SqlParameter("@grade", grade));

            SqlParameter pOutmsg = new SqlParameter("@outmsg", SqlDbType.VarChar, 256);
            pOutmsg.Direction = ParameterDirection.Output;
            parms.Add(pOutmsg);

            SqlParameter pNewid = new SqlParameter("@newid", SqlDbType.Int, 4);
            pNewid.Direction = ParameterDirection.Output;
            parms.Add(pNewid);
            SqlParameter pRetval = new SqlParameter("@retval", SqlDbType.Int, 4);
            pRetval.Direction = ParameterDirection.ReturnValue;
            parms.Add(pRetval);


            db.execute_procedure("p_system_option_save", parms);

            outmsg = (pOutmsg.Value == System.DBNull.Value) ? "" : Convert.ToString(pOutmsg.Value);
            newid = (pNewid.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pNewid.Value);

            int retval = (pRetval.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pRetval.Value);

        }

                                                        public static SystemOptionInfo p_system_option_detail(string item, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@item", item));
            SqlParameter pRetval = new SqlParameter("@retval", SqlDbType.Int, 4);
            pRetval.Direction = ParameterDirection.ReturnValue;
            parms.Add(pRetval);


            db.fetch_procedure("p_system_option_detail", parms);

            int retval = (pRetval.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pRetval.Value);

            if (db.num_rows == 0)
            {
                return null;
            }
            else
            {
                SystemOptionInfo c = new SystemOptionInfo();
                c.Typeid = (db.rows[0]["typeid"] == System.DBNull.Value) ? 0 : Convert.ToInt32(db.rows[0]["typeid"].ToString());
                c.DltFlg = (db.rows[0]["dltFlg"] == System.DBNull.Value) ? 0 : Convert.ToInt32(db.rows[0]["dltFlg"].ToString());
                c.ID = (db.rows[0]["ID"] == System.DBNull.Value) ? 0 : Convert.ToInt32(db.rows[0]["ID"].ToString());
                c.SubsidiaryID = (db.rows[0]["subsidiaryID"] == System.DBNull.Value) ? 0 : Convert.ToInt32(db.rows[0]["subsidiaryID"].ToString());
                c.ItemValue = (db.rows[0]["itemValue"] == System.DBNull.Value) ? 0 : Convert.ToInt32(db.rows[0]["itemValue"].ToString());
                c.Createdate = (db.rows[0]["createdate"] == System.DBNull.Value) ? DateTime.MinValue : Convert.ToDateTime(db.rows[0]["createdate"].ToString());
                c.Modifydate = (db.rows[0]["modifydate"] == System.DBNull.Value) ? DateTime.MinValue : Convert.ToDateTime(db.rows[0]["modifydate"].ToString());
                c.Subsidiary = (db.rows[0]["subsidiary"] == System.DBNull.Value) ? "" : db.rows[0]["subsidiary"].ToString();
                c.Item = (db.rows[0]["item"] == System.DBNull.Value) ? "" : db.rows[0]["item"].ToString();
                c.ItemFieldName = (db.rows[0]["itemFieldName"] == System.DBNull.Value) ? "" : db.rows[0]["itemFieldName"].ToString();
                c.ItemField = (db.rows[0]["itemField"] == System.DBNull.Value) ? "" : db.rows[0]["itemField"].ToString();
                c.ModifyUser = (db.rows[0]["modifyUser"] == System.DBNull.Value) ? "" : db.rows[0]["modifyUser"].ToString();
                c.Remark = (db.rows[0]["remark"] == System.DBNull.Value) ? "" : db.rows[0]["remark"].ToString();
                return c;
            }
        }
    }
}
